package com.zhu.commentstatisticspublisher.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.zhu.commentstatisticspublisher.bean.UserEvaluate;
import com.zhu.commentstatisticspublisher.mapper.EmotionRecognitionMapper;
import com.zhu.commentstatisticspublisher.service.EmotionRecognitionService;
import com.zhu.commentstatisticspublisher.utils.GetDateUtil;
import com.zhu.commentstatisticspublisher.utils.ScriptCall;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

@Service
public class EmotionRecognitionServiceImpl implements EmotionRecognitionService {

    @Autowired
    private EmotionRecognitionMapper emotionRecognitionMapper;

    private String recognition;

    @DS("setSecondDataSource")
    @Override
    public List<UserEvaluate> getEvaluateUsers(String emotionVal,String date) {
        return emotionRecognitionMapper.getEvaluateUsers(emotionVal,date);
    }

    /**
     * 情感识别
     * @param userEvaluate 评论对象
     * @return
     */
    @DS("setSecondDataSource")
    @Override
    public int commentSubmit(UserEvaluate userEvaluate)  {
        int submit = 0;
        Properties properties = new Properties();
        String comment = userEvaluate.getComment();
        ArrayList<String>  parameters = new ArrayList<>();   //传入脚本参数
        //python
        try {
            ClassLoader classLoader = EmotionRecognitionServiceImpl.class.getClassLoader();  //加载配置
            InputStream inputStream = classLoader.getResourceAsStream("script.properties");
            properties.load(inputStream);

            String pythonVersion = properties.getProperty("script.version");
            String scriptPath = properties.getProperty("script.scriptPath");

            ScriptCall<Integer> pythonScriptCall = new ScriptCall<>(properties.getProperty("script.environment"));
            parameters.add(comment);
            ArrayList<Integer> recognitionResult = pythonScriptCall.executePythonScript(pythonVersion, scriptPath, parameters);
            System.out.println(recognitionResult);
            for (Integer integer : recognitionResult) {   //model 只会有一个输出
                if (integer == 1){
                    userEvaluate.setEmotion("正向");
                   // recognition = "正向";
                    break;
                }else if (integer == 0){
                    userEvaluate.setEmotion("负向");
                   // recognition = "负向";
                    break;
                }else {
                    throw new RuntimeException("Model has an exception");
                }
            }
            //mapper
            String createTime = GetDateUtil.dateFormat(userEvaluate.getCreateTime());
            submit = emotionRecognitionMapper.commentsSubmit(userEvaluate.getUserId(),comment,userEvaluate.getEmotion(),createTime);
        }catch (IOException e){
            e.printStackTrace();
        }
        return submit;
    }


}
